package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.example.myapplication.cgq.cgq1;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class weather extends AppCompatActivity {

    private TextView zwx1;
    private TextView zwx2;
    private TextView kq1;
    private TextView kq2;
    private TextView yd1;
    private TextView yd2;
    private TextView cy1;
    private TextView cy2;
    private TextView gm;
    private TextView gm2;
    private TextView xc1;
    private TextView xc2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        zwx1 = findViewById(R.id.zwx1);
        zwx2 = findViewById(R.id.zwx2);
        kq1 = findViewById(R.id.kq1);
        kq2 = findViewById(R.id.kq2);
        yd1 = findViewById(R.id.yd1);
        yd2 = findViewById(R.id.yd2);
        cy1 = findViewById(R.id.cy1);
        cy2 = findViewById(R.id.cy2);
        gm = findViewById(R.id.gm);
        gm2 = findViewById(R.id.gm2);
        xc1 = findViewById(R.id.xc1);
        xc2 = findViewById(R.id.xc2);

        new Thread(mrun).start();


    }

    private void getList() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request build = new Request.Builder().url("https://mock.w2code.com/api/cgq").get().build();
        okHttpClient.newCall(build).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                cgq1 cgq1 = new Gson().fromJson(string, cgq1.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        zwx1.setText(cgq1.data.list.guangzhaoqiangdu);
                        if(Integer.valueOf(cgq1.data.list.guangzhaoqiangdu)<1000){
                            zwx2.setText("弱");
                        }
                        if(Integer.valueOf(cgq1.data.list.guangzhaoqiangdu)>1000 && Integer.valueOf(cgq1.data.list.guangzhaoqiangdu)<3000){
                            zwx2.setText("中等");
                        }
                        if(Integer.valueOf(cgq1.data.list.guangzhaoqiangdu)>3000){
                            zwx2.setText("强");
                        }

                    }
                });


            }
        });
    }

    private Handler mhandle = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            getList();
        }
    };

    private Runnable mrun = new Runnable() {
        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mhandle.sendMessage(mhandle.obtainMessage());
            }
        }
    };
}